package edu.whu.demo.job;

import edu.whu.demo.dao.UserRegisterDao;
import edu.whu.demo.domain.UserRegister;
import edu.whu.demo.service.impl.UserRegisterServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ClearSchedule extends QuartzJobBean {
    @Autowired
    UserRegisterServiceImpl userRegisterService;
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext){
        List<UserRegister> userRegisters = userRegisterService.getAllUserRegister();
        for (UserRegister userRegister : userRegisters) {
            if (checkTimeOut(userRegister.getTime())){
                //确认超时
                userRegisterService.deleteUserRegisterOk(userRegister.getName());
            }
        }
        System.out.println("执行清除任务");
    }

    private boolean checkTimeOut(String time){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date s1;//超时时间点
        Date s2 = new Date();//现在的时间
        try {
            s1 = sdf1.parse(time);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        long time1 = s1.getTime();
        long time2 = s2.getTime();
        return time2 >= time1;//超时时间已经到了
    }

    private String  fiveNextTime(){
        Date date = new Date();
        Calendar cal = Calendar.getInstance();//创建时间工具类
        cal.setTime(date);//设置时间坐标
        cal.add(Calendar.MINUTE,5 );//给时间坐标加上minutes分钟
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(cal.getTime());
    }
}